#!/bin/csh -f
#
# aq2x: Shell script to convert Aqua restraint file to other format.
# ===========================================================
# Ton Rullmann, Mar 14, 1997 - Aqua 1.2
# ===========================================================
#
# The variable 'aquaroot' must exist and point to the Aqua top directory.
# The 'aqsetup' script should reside in that directory.
#

if ( $#argv < 4 ) goto usage
goto begin

#####################
# usage block
#####################
usage:
    echo 'usage:'
    echo 'aq2x  [-n name_format] [-terseness value]'
    echo '      output_format  input_file  output_file  coordinates_file'
    echo '   -terseness value: [0-9] integer specifying decreasing verbosity'
    exit (1)

#####################
# initialization
#####################
begin:
    set script_log = "aq2x.log"
    setenv AQUATERSENESS 0     # most verbose messages
    
    if ( ${?fil_type} ) then
        echo 'Using for file type:' $fil_type
    else
        set fil_type = noe
    endif
    
    set errstatus = 0
    set namefmt   = ""
    set title     = ""
    set oldmap    = "off"

#####################
# process the options
#####################
options:
switch ( "$1" )
    case "-n":
        if ( $#argv < 6 ) goto usage
        shift
	set namefmt = $1
	shift
        breaksw
    case "-terseness":
	shift        
	setenv AQUATERSENESS $1 # Inherited by all processes because using setenv
	shift
	breaksw
    default:
        goto endoptions
endsw
goto options

endoptions:
    if ( $#argv < 4 ) goto usage

    # Check options for acceptable ranges
    if ( ( $AQUATERSENESS < 0 ) || ( $AQUATERSENESS >9 ) ) then
        echo "The parameter terseness (specified to be $AQUATERSENESS) can only"
        echo "take integer values from 0 through 9"
        exit(1)
    endif

    # Set level of terseness as a category which can be modified later on
    if ( $AQUATERSENESS < 3 ) then
        set terselevel = "verbose"
    else if ( $AQUATERSENESS < 6 ) then
        set terselevel = "curbed"
    else
        set terselevel = "terse"
    endif

    # get started
    # Initialize the log file
    \rm $script_log >& /dev/null
    
    if ( $terselevel != "verbose" ) then
        echo 'USE A LOWER VALUE FOR TERSENESS TO GET MORE OUTPUT IN THIS FILE IF NEEDED'    >> $script_log
        echo ' '                                                                            >> $script_log
    else
        echo 'Aqua program directory:' $aquaroot
        echo 'AQ2X - Converting distance restraints'
    endif

    source $aquaroot/aqsetup

# set output format
    set outfmt = $1
    switch ( "$outfmt" )
	case "aqua":
	case "nmrstar":
	    if ( "$namefmt" == "" ) then
		set namefmt = "aqua"
	    endif
	    breaksw
	case "diana":
	case "xplor":
	case "biosym":
	    if ( "$namefmt" != "" ) then
		echo 'AQ2X - Ignoring name format' $namefmt
		set namefmt = ""
	    endif
	    breaksw
	default:
	    echo '** AQ2X - Output format' $outfmt 'not (yet) implemented'
	    exit (1)
    endsw
# set restraints input file
    set inrestrs = $2
    if ( ! -e $inrestrs ) then
        echo '** AQ2X - File' $inrestrs 'does not exist'
        exit (1)
    endif
# set restraints output file
    set outrestrs = $3
# set coordinates file
    set coords = $4
    if ( ! -e $coords ) then
        echo '** AQ2X - File' $coords 'does not exist'
        exit (1)
    endif

# get started

# set title
	if ( "$title" == "" ) then
	    if ( "$namefmt" == "" ) then
		set title = "' Converted from $inrestrs (AQUA version $AQUAVERS)'"
	    else
		set title = "' Converted from $inrestrs using name format $namefmt (AQUA version $AQUAVERS)'"
	    endif
	endif
# setup project name
    set proj = `qgetproj $coords`
    if ( $terselevel == "verbose" ) then
        echo 'AQ2X - Project name is' $proj
    endif
    qproject $proj

    if ( $terselevel == "verbose" ) then
        echo 'AQ2X - Converting distance restraints '       >> $script_log
        echo '-------------------------------------'        >> $script_log
        echo ' '                                            >> $script_log
        echo 'AQ2X - Aqua program directory:' $aquaroot     >> $script_log
        echo ' '                                            >> $script_log
    endif
# first read the required characteristics from the restraint file
    set fmtline = (`sed -n '2p;2q' $inrestrs`)
    if ( ( $#fmtline == 4 ) || ( $#fmtline == 5 ) ) then
        set infmt = $fmtline[4]
    else
        echo 'The input restraint file did not contain the correct(-ly formatted) information'
        echo 'on the second line. Is it an AQUA formatted restraint file?'
        goto endaq2x
    endif
    
# determine the coordinate file type
    qguessc $coords >! qguessc.log
    set sts = $status
    if ( $terselevel == "verbose" ) then
	cat qguessc.log
	echo '*'        >> aqpc.log            
	cat qguessc.log >> aqpc.log
	echo '*'        >> aqpc.log            
    endif
    \rm qguessc.log    
    if ( $sts == 0 ) then
        echo "* ERROR: could not determine coordinate file type"
        exit( 1 )
    else
	set coordinp = $sts
    endif
#    echo '#JFD April 2, 2008 hard override setting coordinp to 4 for xplor'.
#    set coordinp = 4
# make or modify the aqua.cmm file
    touch aqua.cmm
    gawk -f $AQUASCRIPTSDIR/makecmm -v coordinp=$coordinp -v restrinp=$infmt \
	-v oldmap=$oldmap -v mr=0 -v terseness=$AQUATERSENESS \
        aqua.cmm > aqua.cmm.tmp
    \mv aqua.cmm.tmp aqua.cmm
    grep 'set *sort' aqua.cmm > /dev/null
    @ sort = !($status)
# now do the work
    if ( $terselevel == "verbose" ) then
        echo '*' >> $script_log
        echo '* Processing coordinate file' $coords 'into Aqua file' >> $script_log
        echo '*' >> $script_log
    endif
    qwhat set batch on exec aqua readone $coords write tmp quit >> $script_log
    set errstatus = $status
    if ( $errstatus ) goto endaq2x
    set sortcom = ""
    if ( $sort ) then
	set sortcom = "sort"
    endif
    if ( $terselevel == "verbose" ) then
        echo '*' >> $script_log
        echo '* Processing restraint file' $inrestrs >> $script_log
        echo '*' >> $script_log
    endif
    qhow set batch on exec aqua mol orig tmp $fil_type aqua $inrestrs $sortcom out $outfmt $namefmt $outrestrs $title quit >> $script_log
    set errstatus = $status
# show important messages and cleanup
endaq2x:
    \rm $proj'_orig_tmp.set' >& /dev/null
    echo ' '
    grep '^ *\*' $script_log
    echo ' '
    echo "AQ2X - Log file: $script_log"
    echo ' '
    if ( $errstatus ) then
        echo "ERROR - quitting"
        exit (1)
    endif
